package com.ray.activiti.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.activiti.common.Constant;
import com.ray.activiti.service.ExtendActNodeuserService;
import com.ray.activiti.table.entity.ExtendActNodeuser;
import com.ray.activiti.table.mapper.ExtendActNodeuserMapper;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.Result;


/**
 * 
 * @Auther ray  qray686898@163.com
 * @Date 2017/12/28
 * @代码参考或来自  https://github.com/huangxianyuan/hxyFrame
 *
 */
@Service
public class ExtendActNodeuserServiceImpl implements ExtendActNodeuserService {
	@Autowired
	private ExtendActNodeuserMapper extendActNodeuserMapper;
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ExtendActNodeuser queryObject(String id){
		return extendActNodeuserMapper.queryObject(id);
	}
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ExtendActNodeuser> queryList(Map<String, Object> map){
		return extendActNodeuserMapper.queryList(map);
	}
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public int queryTotal(Map<String, Object> map){
		return extendActNodeuserMapper.queryTotal(map);
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void save(ExtendActNodeuser extendActNodeuser){
		extendActNodeuserMapper.save(extendActNodeuser);
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void update(ExtendActNodeuser extendActNodeuser){
		extendActNodeuserMapper.update(extendActNodeuser);
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void delete(String id){
		extendActNodeuserMapper.delete(id);
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void deleteBatch(String[] ids){
		extendActNodeuserMapper.deleteBatch(ids);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<String> queryIds(String type, String nodeId) {
		ExtendActNodeuser params = new ExtendActNodeuser();
		params.setSelectType(type);
		params.setNodeId(nodeId);
		List<String> ids = new ArrayList<>();
		for(ExtendActNodeuser user :extendActNodeuserMapper.queryListByBean(params)){
			ids.add(user.getSelectId());
		}
		return ids;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public Result<Object> addUser(String userId, String nodeId, LoginUser user) {
		ExtendActNodeuser params = new ExtendActNodeuser();
		params.setNodeId(nodeId);
		params.setSelectId(userId);
		params.setSelectType(Constant.SelectType.USER.getValue());
		List<ExtendActNodeuser> actNodeusers = extendActNodeuserMapper.queryListByBean(params);
		if(!actNodeusers.isEmpty()){
			return new Result<>(-1, String.format("%s已经选择", userId));
		}
		ExtendActNodeuser entity = new ExtendActNodeuser();
		entity.setId(UUIDUtil.getUUID());
		entity.setNodeId(nodeId);
		entity.setSelectId(userId);
		entity.setSelectType(Constant.SelectType.USER.getValue());
		if(extendActNodeuserMapper.save(entity)==1){
			return new Result<>(0, "保存成功");
		}
		return new Result<>(-1, "保存失败");
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public Result<Object> deleteUser(String userId, String nodeId, LoginUser user) {
		ExtendActNodeuser params = new ExtendActNodeuser();
		params.setNodeId(nodeId);
		params.setSelectId(userId);
		params.setSelectType(Constant.SelectType.USER.getValue());
		List<ExtendActNodeuser> actNodeusers = extendActNodeuserMapper.queryListByBean(params);
		if(actNodeusers.isEmpty()){
			return new Result<>(-1, String.format("%s未选择", userId));
		}
		ExtendActNodeuser entity = actNodeusers.get(0);
		if(extendActNodeuserMapper.delete(entity.getId())==1){
			return new Result<>(0, "删除成功");
		}
		return new Result<>(-1, "删除失败");
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public Result<Object> addRole(String roleId, String nodeId, LoginUser user) {
		ExtendActNodeuser params = new ExtendActNodeuser();
		params.setNodeId(nodeId);
		params.setSelectId(roleId);
		params.setSelectType(Constant.SelectType.ROLE.getValue());
		List<ExtendActNodeuser> actNodeusers = extendActNodeuserMapper.queryListByBean(params);
		if(!actNodeusers.isEmpty()){
			return new Result<>(-1, String.format("%s已经选择", roleId));
		}
		ExtendActNodeuser entity = new ExtendActNodeuser();
		entity.setId(UUIDUtil.getUUID());
		entity.setNodeId(nodeId);
		entity.setSelectId(roleId);
		entity.setSelectType(Constant.SelectType.ROLE.getValue());
		if(extendActNodeuserMapper.save(entity)==1){
			return new Result<>(0, "保存成功");
		}
		return new Result<>(-1, "保存失败");
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public Result<Object> deleteRole(String roleId, String nodeId, LoginUser user) {
		ExtendActNodeuser params = new ExtendActNodeuser();
		params.setNodeId(nodeId);
		params.setSelectId(roleId);
		params.setSelectType(Constant.SelectType.ROLE.getValue());
		List<ExtendActNodeuser> actNodeusers = extendActNodeuserMapper.queryListByBean(params);
		if(actNodeusers.isEmpty()){
			return new Result<>(-1, String.format("%s未选择", roleId));
		}
		ExtendActNodeuser entity = actNodeusers.get(0);
		if(extendActNodeuserMapper.delete(entity.getId())==1){
			return new Result<>(0, "删除成功");
		}
		return new Result<>(-1, "删除失败");
	}

}
